// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import gameControl from "./gameControl";
// import AudioManage, {soundList} from "../resources/framework/AudioManage";
import gameData from "./gameData";
import AudioManage, {soundList} from "../framework/AudioManage";
import GameConfig from "../framework/GameConfig";
import Widget = cc.Widget;
import appConfig from "../framework/appConfig";

export enum JoystickType{
    FIXED = 0,
    FOLLOW = 1,
    FRIST = 2
}

const {ccclass, property} = cc._decorator;

@ccclass
export default class keyControl extends cc.Component {

    @property({displayName : "移动速度"})
    moveSpeed : cc.Vec2 = cc.v2(500,900);

    @property({displayName : "开启发送点击消息"})
    openSendClickListen : boolean = false;

    @property(cc.Node)
    roleNode : cc.Node = null;

    @property({displayName : "键盘的是否禁用"})
    shieldKey : boolean = false;

    @property({displayName : "是否开启反转"})
    openReversal : boolean = false;

    @property({displayName : "幽灵模式"})
    ghostModel : boolean = false;

    @property({displayName : "最高层级"})
    highLayer : boolean = false;

    curJoystickType : JoystickType = JoystickType.FOLLOW;

    isMovingList : boolean[] = [false,false,false];

    IDList = {
        "up" :  null,
        "left" : null,
        "right" : null
    };

    isGameOver : boolean = false;

    isSendMessage = false;

    isReserve : boolean = false;

    static dropTable : boolean = false;

    gan : cc.Node = null;

    bottom : cc.Node = null;

    initSpeed = cc.v2(0,0);

    _stickPos = null;

    _touchLocation = null;

    Joystick : cc.Node = null;

    _radius = 0;

    protected onEnable(): void {
        keyControl.dropTable = true;

        if(this.highLayer){
            //this.node.zIndex = cc.find("Canvas/gameUI").zIndex + 1;
        }
    }

    initKeyList(){
        this.Joystick = this.node.getChildByName("Joystick");
        this.gan = this.Joystick.getChildByName("gan");
        this.bottom = this.Joystick.getChildByName("bottom");
        let mask = this.bottom.getChildByName("mask");
        let pic = this.bottom.getChildByName("Joystick_0");

        mask.active = false;
        pic.opacity = 100;
        this.Joystick.height = 325;
        this.bottom.setPosition(cc.v2(-420,-25));
        this.gan.setPosition(cc.v2(-420,-25));

        let widget = this.Joystick.addComponent(cc.Widget);
        widget.alignMode = Widget.AlignMode.ALWAYS;
        widget.isAlignBottom = true;
        widget.bottom = 0;
        widget.updateAlignment();
    }

    onLoad () {

        keyControl.dropTable = false;

        this.initSpeed.x = this.moveSpeed.x;
        this.initSpeed.y = this.moveSpeed.y;

        this.node.width = 1650;
        this.node.getChildByName("up").getChildByName("bg").active = false;
        this.node.getChildByName("hand").getChildByName("bg").active = false;

        //获取遥感节点
        this.Joystick = this.node.getChildByName("Joystick");
        this.gan = this.Joystick.getChildByName("gan");
        this.bottom = this.Joystick.getChildByName("bottom");

        //初始化
        this.initKeyList();

        this._radius = this.bottom.width / 8;

        if(appConfig.gamePlatform() == 0){

            this.Joystick.on(cc.Node.EventType.TOUCH_START, this.HorizontalTouchStart, this);

            this.Joystick.on(cc.Node.EventType.TOUCH_MOVE, this.HorizontalTouchMove, this);

            this.Joystick.on(cc.Node.EventType.TOUCH_END, this.HorizontalTouchEnd, this);

            this.Joystick.on(cc.Node.EventType.TOUCH_CANCEL, this.HorizontalTouchCancel, this);

            this.node.getChildByName("up").on("touchstart",this.VerticalTouchStart,this);
            this.node.getChildByName("up").on("touchmove",this.VerticalTouchMove,this);
            this.node.getChildByName("up").on("touchend",this.VerticalTouchEnd,this);

        }else{
            cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, (event)=>{
                if(this.shieldKey) return;
                switch(event.keyCode) {
                    case cc.macro.KEY.a || cc.macro.KEY.left:
                        if(this.openSendClickListen){
                            cc.game.emit("receiveKeyEvent");
                        }
                        this.isMovingList[2] = true;
                        break;
                    case cc.macro.KEY.d || cc.macro.KEY.right:
                        if(this.openSendClickListen){
                            cc.game.emit("receiveKeyEvent");
                        }
                        this.isMovingList[1] = true;
                        break;
                    case cc.macro.KEY.w || cc.macro.KEY.up:
                        this.node.getChildByName("up").getChildByName("bg").active = false;
                        if(this.openSendClickListen){
                            cc.game.emit("receiveKeyEvent");
                        }
                        this.isMovingList[0] = true;
                        break;
                }
            }, this);

            cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, (event)=>{
                switch(event.keyCode) {
                    case cc.macro.KEY.a || cc.macro.KEY.left:
                        this.isMovingList[2] = false;
                        break;
                    case cc.macro.KEY.d || cc.macro.KEY.right:
                        this.isMovingList[1] = false;

                        break;
                    case cc.macro.KEY.w || cc.macro.KEY.up:
                        this.node.getChildByName("up").getChildByName("bg").active = true;
                        this.isMovingList[0] = false;
                        break;
                }
            }, this);
        }

        this.registerEvent();
    }

    registerEvent(){
        cc.game.on("setHandStatus",(show,action,target)=>{
            if(show){
                this.node.getChildByName("hand").active = true;
            }else{
                this.node.getChildByName("hand").active = false;
            }
            cc.game.emit("setHandAction",action,target);
        });

        cc.game.on("shieldPressKey",(res)=>{
            if(res){
                this.shieldKey = true;
                for(let i in this.isMovingList){
                    this.isMovingList[i] = false;
                }
                this.roleNode.getComponent(cc.RigidBody).linearVelocity = cc.v2(0,0);
            }else{
                this.shieldKey = false;
            }
        });

        cc.game.on("closeClickListen",(res?)=>{
            this.openSendClickListen = false;
        });

        cc.game.on("GameOver",()=>{
            this.isGameOver = true;
            cc.director.getPhysicsManager().enabled = false;
            AudioManage.playSFX(soundList.die);
            AudioManage.playSFX(soundList.walk,true,true,1);
        });

        cc.game.on("gameReserve",(res)=>{
            if(res == 1){
                this.openReversal = this.isReserve;
            }else{
                this.openReversal = false;
            }
            if(this.openReversal){
                this.roleNode.getComponent(cc.RigidBody).gravityScale = -Math.abs(this.roleNode.getComponent(cc.RigidBody).gravityScale);
                this.roleNode.angle = 180;
                for(let i in cc.find("Canvas/petGroup").children){
                    cc.find("Canvas/petGroup").children[i].angle = 180;
                }
            }else{
                this.roleNode.getComponent(cc.RigidBody).gravityScale = Math.abs(this.roleNode.getComponent(cc.RigidBody).gravityScale);
                this.roleNode.angle = 0;
                for(let i in cc.find("Canvas/petGroup").children){
                    cc.find("Canvas/petGroup").children[i].angle = 0;
                }
            }
        });

        cc.game.on("setReserveState",()=>{
            this.isReserve = !this.isReserve;
        });

        cc.game.on("changeRoleStatus",(res)=>{
            if(typeof res != "undefined"){
                this.ghostModel = res;
                this.isMovingList = [false,false,false];
            }
        })
    }

    protected onDestroy(): void {
        cc.game.off("setHandStatus");
        cc.game.off("shieldPressKey");
        cc.game.off("closeClickListen");
        cc.game.off("GameOver");
        cc.game.off("gameReserve");
        cc.game.off("setReserveState");
        cc.game.off("changeRoleStatus");
        AudioManage.playSFX(soundList.walk,true,true,1);
    }

    private startPos:any;
    HorizontalTouchStart(event){

        if(this.shieldKey) return;
        if(this.openSendClickListen && !this.isSendMessage){
            cc.game.emit("receiveKeyEvent");
            this.isSendMessage = true;
        }
        const touchPos = this.Joystick.convertToNodeSpaceAR(event.getLocation());
        this.startPos = event.getLocation();

        if (this.curJoystickType === JoystickType.FIXED) {

            this._stickPos = this.bottom.getPosition();

            // 触摸点与圆圈中心的距离
            const distance = touchPos.sub(this.bottom.getPosition()).mag();

            // 手指在圆圈内触摸,控杆跟随触摸点
            if (this._radius > distance) {
                this.gan.setPosition(touchPos);
            }

        } else if (this.curJoystickType === JoystickType.FOLLOW ) {

            // 记录摇杆位置，给 touch move 使用
            this._stickPos = touchPos;
            this.node.opacity = 255;
            this._touchLocation = event.getLocation();

            // 更改摇杆的位置
            this.bottom.setPosition(touchPos);
            this.gan.setPosition(touchPos);

        }else if(this.curJoystickType === JoystickType.FRIST) {
            // 记录摇杆位置，给 touch move 使用
            this._stickPos = touchPos;
            this.node.opacity = 255;
            this._touchLocation = event.getLocation();

            // 更改摇杆的位置
            this.bottom.setPosition(touchPos);
            this.gan.setPosition(touchPos);
        }
    }


    lastPosition = cc.v2(0,0);
    HorizontalTouchMove(event) {

        if(this.shieldKey) return;

        if (this.curJoystickType === JoystickType.FOLLOW) {
            // 如果 touch start 位置和 touch move 相同，禁止移动
            if (this._touchLocation === event.getLocation()) {
                return false;
            }
        }

        // 以圆圈为锚点获取触摸坐标
        const touchPos = this.bottom.convertToNodeSpaceAR(event.getLocation());
        const distance = touchPos.mag();

        // 由于摇杆的 postion 是以父节点为锚点，所以定位要加上 touch start 时的位置
        const posX = this._stickPos.x + touchPos.x;
        const posY = this._stickPos.y + touchPos.y;

        //let deletX = event.getDelta();
        let deletX = event.getLocation().x - this.startPos.x;

        // console.log(deletX,"deletX============>")
        // 归一化
        const p = cc.v2(posX, posY).sub(this.bottom.getPosition()).normalize();

        if (this._radius > distance) {

            this.gan.setPosition(cc.v2(posX, posY));

        } else {
            // 控杆永远保持在圈内，并在圈内跟随触摸更新角度
            const x = this._stickPos.x + p.x * this._radius;
            const y = this._stickPos.y + p.y * this._radius;

            this.gan.setPosition(cc.v2(x, y));
        }

        // this.moveSpeed.x = this.initSpeed.x *  Math.abs(p.x);

         if(deletX > 0){
            this.isMovingList[1] = true;
            this.isMovingList[2] = false;
         }else if(deletX < 0){
            this.isMovingList[1] = false;
            this.isMovingList[2] = true;
         }else{
            this.isMovingList[2] = false;
            this.isMovingList[1] = false;
         }
    }

    HorizontalTouchEnd(event){
        // if(this.shieldKey) return;
        this.isMovingList[2] = false;
        this.isMovingList[1] = false;

        this.gan.setPosition(this.bottom.getPosition());
        if (this.curJoystickType == JoystickType.FOLLOW) {
            this.JoystickReset();
        }
    }

    HorizontalTouchCancel(event){
        // if(this.shieldKey) return;

        this.isMovingList[2] = false;
        this.isMovingList[1] = false;

        this.gan.setPosition(this.bottom.getPosition());
        if (this.curJoystickType == JoystickType.FOLLOW) {
            this.JoystickReset();
        }

    }

    JoystickReset(){
        this.bottom.setPosition(cc.v2(-420,-25));
        this.gan.setPosition(cc.v2(-420,-25));
    }



    VerticalTouchStart(event){
        if(this.shieldKey) return;
        if(this.openSendClickListen && !this.isSendMessage){
            cc.game.emit("receiveKeyEvent");
            this.isSendMessage = true;
        }
        let worldPoint = event.getLocation();
        let pos = this.node.getChildByName("up").convertToNodeSpaceAR(worldPoint);
        let upNode = this.node.getChildByName("up");
        if(cc.rect(- upNode.width / 2,- upNode.height / 2,upNode.width,upNode.height).contains(pos)){
            upNode.getChildByName("bg").active = true;
            this.isMovingList[0] = true;
        }
    }

    VerticalTouchMove(event){
        if(this.shieldKey) return;
        let worldPoint = event.getLocation();
        let pos = this.node.getChildByName("up").convertToNodeSpaceAR(worldPoint);
        let upNode = this.node.getChildByName("up");
        if (cc.rect(- upNode.width / 2, - upNode.height / 2, upNode.width, upNode.height).contains(pos)) {
            this.isMovingList[0] = true;
            upNode.getChildByName("bg").active = true;
        } else {
            this.isMovingList[0] = false;
            upNode.getChildByName("bg").active = false;
        }
    }

    VerticalTouchEnd(event){
        if(this.shieldKey) return;
        let worldPoint = event.getLocation();
        let pos = this.node.getChildByName("up").convertToNodeSpaceAR(worldPoint);
        let upNode = this.node.getChildByName("up");
        if(cc.rect(- upNode.width / 2,- upNode.height / 2,upNode.width,upNode.height).contains(pos)){
            this.isMovingList[0] = false;
            upNode.getChildByName("bg").active = false;
        }
    }

    isWalk = false;
    curAnimation = 0;
    update(dt: number): void {
        if(this.isGameOver) return;
        let speed = this.roleNode.getComponent(cc.RigidBody).linearVelocity;
        if(this.isMovingList[1]){
            if(this.openReversal){
                if(this.ghostModel){
                    this.roleNode.getChildByName("spine").scaleX = -0.4;
                    this.roleNode.getChildByName("spine").scaleY = 0.4;
                }else{
                    this.roleNode.getChildByName("spine").scaleX = -0.3;
                    this.roleNode.getChildByName("spine").scaleY = 0.3;
                }
                this.roleNode.getComponent(cc.RigidBody).linearVelocity = cc.v2(this.moveSpeed.x,speed.y);
                this.roleNode.getChildByName("dust").x = Math.abs(this.roleNode.getChildByName("dust").x) * -1;


                for(let i in gameData.getPetCurSkin()){
                    this.roleNode.getChildByName("petNode" + (1 + parseInt(i))).x = Math.abs(this.roleNode.getChildByName("petNode" +(1 + parseInt(i))).x) * -1;
                    let petGroup = cc.find("Canvas/petGroup").children;
                    petGroup[i].getChildByName("spine").scaleX = Math.abs(petGroup[i].getChildByName("spine").scaleX) * -1;
                }
                if(this.roleNode.getChildByName("petNode8")){
                    this.roleNode.getChildByName("petNode8").x = Math.abs(this.roleNode.getChildByName("petNode8").x) * -1;
                    cc.find("Canvas/petGroup").children[7].getChildByName("spine").scaleX = Math.abs(cc.find("Canvas/petGroup").children[7].getChildByName("spine").scaleX) * -1;
                }
            }else{
                if(this.ghostModel){
                    this.roleNode.getChildByName("spine").scaleX = 0.4;
                    this.roleNode.getChildByName("spine").scaleY = 0.4;
                }else{
                    this.roleNode.getChildByName("spine").scaleX = 0.3;
                    this.roleNode.getChildByName("spine").scaleY = 0.3;
                }
                this.roleNode.getComponent(cc.RigidBody).linearVelocity = cc.v2(this.moveSpeed.x,speed.y);
                this.roleNode.getChildByName("dust").x = Math.abs(this.roleNode.getChildByName("dust").x) * -1;

                for(let i in gameData.getPetCurSkin()){
                    this.roleNode.getChildByName("petNode" + (1 + parseInt(i))).x = Math.abs(this.roleNode.getChildByName("petNode" +(1 + parseInt(i))).x) * -1;
                    let petGroup = cc.find("Canvas/petGroup").children;
                    petGroup[i].getChildByName("spine").scaleX = Math.abs(petGroup[i].getChildByName("spine").scaleX);
                }

                if(this.roleNode.getChildByName("petNode8")){
                    this.roleNode.getChildByName("petNode8").x = Math.abs(this.roleNode.getChildByName("petNode8").x) * -1;
                    cc.find("Canvas/petGroup").children[7].getChildByName("spine").scaleX = Math.abs(cc.find("Canvas/petGroup").children[7].getChildByName("spine").scaleX);
                }
            }
        }

        if(this.isMovingList[2]){
            if(this.openReversal){
                if(this.ghostModel){
                    this.roleNode.getChildByName("spine").scaleX = 0.4;
                    this.roleNode.getChildByName("spine").scaleY = 0.4;
                }else{
                    this.roleNode.getChildByName("spine").scaleX = 0.3;
                    this.roleNode.getChildByName("spine").scaleY = 0.3;
                }
                this.roleNode.getComponent(cc.RigidBody).linearVelocity = cc.v2(-this.moveSpeed.x,speed.y);
                this.roleNode.getChildByName("dust").x = Math.abs(this.roleNode.getChildByName("dust").x);

                for(let i in gameData.getPetCurSkin()){
                    this.roleNode.getChildByName("petNode" + (1 + parseInt(i))).x = Math.abs(this.roleNode.getChildByName("petNode" +(1 + parseInt(i))).x);
                    let petGroup = cc.find("Canvas/petGroup").children;
                    petGroup[i].getChildByName("spine").scaleX = Math.abs(petGroup[i].getChildByName("spine").scaleX);
                }
                if(this.roleNode.getChildByName("petNode8")){
                    this.roleNode.getChildByName("petNode8").x = Math.abs(this.roleNode.getChildByName("petNode8").x);
                    cc.find("Canvas/petGroup").children[7].getChildByName("spine").scaleX = Math.abs(cc.find("Canvas/petGroup").children[7].getChildByName("spine").scaleX) * -1;
                }
            }else{
                if(this.ghostModel){
                    this.roleNode.getChildByName("spine").scaleX = -0.4;
                    this.roleNode.getChildByName("spine").scaleY = 0.4;
                }else{
                    this.roleNode.getChildByName("spine").scaleX =-0.3;
                    this.roleNode.getChildByName("spine").scaleY = 0.3;
                }
                this.roleNode.getComponent(cc.RigidBody).linearVelocity = cc.v2(-this.moveSpeed.x,speed.y);
                this.roleNode.getChildByName("dust").x = Math.abs(this.roleNode.getChildByName("dust").x);

                for(let i in gameData.getPetCurSkin()){
                    this.roleNode.getChildByName("petNode" + (1 + parseInt(i))).x = Math.abs(this.roleNode.getChildByName("petNode" +(1 + parseInt(i))).x);
                    let petGroup = cc.find("Canvas/petGroup").children;
                    petGroup[i].getChildByName("spine").scaleX = Math.abs(petGroup[i].getChildByName("spine").scaleX) * -1;
                }
                if(this.roleNode.getChildByName("petNode8")){
                    this.roleNode.getChildByName("petNode8").x = Math.abs(this.roleNode.getChildByName("petNode8").x);
                    cc.find("Canvas/petGroup").children[7].getChildByName("spine").scaleX = Math.abs(cc.find("Canvas/petGroup").children[7].getChildByName("spine").scaleX) * -1;
                }
            }
        }

        if(this.ghostModel){
            let speed = this.roleNode.getComponent(cc.RigidBody).linearVelocity;

            if(!this.isMovingList[1] && !this.isMovingList[2]){
                this.roleNode.getComponent(cc.RigidBody).linearVelocity = cc.v2(0,speed.y);
            }else{
                if(this.isMovingList[1] && !this.isMovingList[2]){
                    this.roleNode.getComponent(cc.RigidBody).linearVelocity = cc.v2(this.moveSpeed.x * 0.5,speed.y);
                }else if(!this.isMovingList[1] && this.isMovingList[2]){
                    this.roleNode.getComponent(cc.RigidBody).linearVelocity = cc.v2(-this.moveSpeed.x * 0.5,speed.y);
                }else{
                    let random = Math.random() > 0.5 ? 1 : -1;
                    this.roleNode.getComponent(cc.RigidBody).linearVelocity = cc.v2(random * this.moveSpeed.x * 0.5,speed.y);
                }
            }
            this.roleNode.getChildByName("dust").active = false;
            if(this.isMovingList[0]){
                // AudioManage.playSFX(soundList.jump,false,false,1);
                let speed = this.roleNode.getComponent(cc.RigidBody).linearVelocity;
                if(!this.openReversal){
                    this.roleNode.getComponent(cc.RigidBody).linearVelocity = cc.v2(speed.x,this.moveSpeed.y * 0.2);
                }else{
                    this.roleNode.getComponent(cc.RigidBody).linearVelocity = cc.v2(speed.x,-this.moveSpeed.y * 0.2);
                }
            }

        }else{
            // let speed = this.roleNode.getComponent(cc.RigidBody).linearVelocity;
            // if(this.isMovingList[1]){
            //     if(this.openReversal){
            //         this.roleNode.getChildByName("spine").scaleX = 0.3 * -1;
            //         this.roleNode.getComponent(cc.RigidBody).linearVelocity = cc.v2(this.moveSpeed.x,speed.y);
            //         this.roleNode.getChildByName("dust").x = Math.abs(this.roleNode.getChildByName("dust").x) * -1;
            //         for(let i in gameData.getPetCurSkin()){
            //             if(parseInt(i) == 0){
            //                 this.roleNode.getChildByName("petNode1").x = Math.abs(this.roleNode.getChildByName("petNode1").x) * -1;
            //             }else {
            //                 this.roleNode.getChildByName("petNode2").x = Math.abs(this.roleNode.getChildByName("petNode2").x) * -1;
            //             }
            //             let petGroup = cc.find("Canvas/petGroup").children;
            //             petGroup[i].getChildByName("spine").scaleX = Math.abs(petGroup[i].getChildByName("spine").scaleX) * -1;
            //         }
            //         if(this.roleNode.getChildByName("petNode3")){
            //             this.roleNode.getChildByName("petNode3").x = Math.abs(this.roleNode.getChildByName("petNode3").x) * -1;
            //             cc.find("Canvas/petGroup").children[2].getChildByName("spine").scaleX = Math.abs(cc.find("Canvas/petGroup").children[2].getChildByName("spine").scaleX) * -1;
            //         }
            //     }else{
            //         this.roleNode.getChildByName("spine").scaleX = 0.3;
            //         this.roleNode.getComponent(cc.RigidBody).linearVelocity = cc.v2(this.moveSpeed.x,speed.y);
            //         this.roleNode.getChildByName("dust").x = Math.abs(this.roleNode.getChildByName("dust").x) * -1;
            //         for(let i in gameData.getPetCurSkin()){
            //             if(parseInt(i) == 0){
            //                 this.roleNode.getChildByName("petNode1").x = Math.abs(this.roleNode.getChildByName("petNode1").x) * -1;
            //             }else {
            //                 this.roleNode.getChildByName("petNode2").x = Math.abs(this.roleNode.getChildByName("petNode2").x) * -1;
            //             }
            //             let petGroup = cc.find("Canvas/petGroup").children;
            //             petGroup[i].getChildByName("spine").scaleX = Math.abs(petGroup[i].getChildByName("spine").scaleX);
            //         }
            //         if(this.roleNode.getChildByName("petNode3")){
            //             this.roleNode.getChildByName("petNode3").x = Math.abs(this.roleNode.getChildByName("petNode3").x) * -1;
            //             cc.find("Canvas/petGroup").children[2].getChildByName("spine").scaleX = Math.abs(cc.find("Canvas/petGroup").children[2].getChildByName("spine").scaleX);
            //         }
            //     }
            // }
            //
            // if(this.isMovingList[2]){
            //     if(this.openReversal){
            //         this.roleNode.getChildByName("spine").scaleX = 0.3;
            //         this.roleNode.getComponent(cc.RigidBody).linearVelocity = cc.v2(-this.moveSpeed.x,speed.y);
            //         this.roleNode.getChildByName("dust").x = Math.abs(this.roleNode.getChildByName("dust").x);
            //         for(let i in gameData.getPetCurSkin()){
            //             if(parseInt(i) == 0){
            //                 this.roleNode.getChildByName("petNode1").x = Math.abs(this.roleNode.getChildByName("petNode1").x);
            //             }else {
            //                 this.roleNode.getChildByName("petNode2").x = Math.abs(this.roleNode.getChildByName("petNode2").x);
            //             }
            //             let petGroup = cc.find("Canvas/petGroup").children;
            //             petGroup[i].getChildByName("spine").scaleX = Math.abs(petGroup[i].getChildByName("spine").scaleX);
            //         }
            //         if(this.roleNode.getChildByName("petNode3")){
            //             this.roleNode.getChildByName("petNode3").x = Math.abs(this.roleNode.getChildByName("petNode3").x);
            //             cc.find("Canvas/petGroup").children[2].getChildByName("spine").scaleX = Math.abs(cc.find("Canvas/petGroup").children[2].getChildByName("spine").scaleX);
            //         }
            //     }else{
            //         this.roleNode.getChildByName("spine").scaleX = -0.3;
            //         this.roleNode.getComponent(cc.RigidBody).linearVelocity = cc.v2(-this.moveSpeed.x,speed.y);
            //         this.roleNode.getChildByName("dust").x = Math.abs(this.roleNode.getChildByName("dust").x);
            //         for(let i in gameData.getPetCurSkin()){
            //             if(parseInt(i) == 0){
            //                 this.roleNode.getChildByName("petNode1").x = Math.abs(this.roleNode.getChildByName("petNode1").x);
            //             }else {
            //                 this.roleNode.getChildByName("petNode2").x = Math.abs(this.roleNode.getChildByName("petNode2").x);
            //             }
            //             let petGroup = cc.find("Canvas/petGroup").children;
            //             petGroup[i].getChildByName("spine").scaleX = Math.abs(petGroup[i].getChildByName("spine").scaleX) * -1;
            //         }
            //         if(this.roleNode.getChildByName("petNode3")){
            //             this.roleNode.getChildByName("petNode3").x = Math.abs(this.roleNode.getChildByName("petNode3").x);
            //             cc.find("Canvas/petGroup").children[2].getChildByName("spine").scaleX = Math.abs(cc.find("Canvas/petGroup").children[2].getChildByName("spine").scaleX) * -1;
            //         }
            //     }
            // }

            if(!this.isMovingList[1] && !this.isMovingList[2]){
                if(this.isWalk){
                    this.isWalk = false;
                }
                let speed = this.roleNode.getComponent(cc.RigidBody).linearVelocity;
                this.roleNode.getComponent(cc.RigidBody).linearVelocity = cc.v2(0,speed.y);
            }else{
                if(!this.isWalk && keyControl.dropTable){
                    this.isWalk = true;
                }
            }

            if((this.isMovingList[1] || this.isMovingList[2]) && keyControl.dropTable){
                this.roleNode.getChildByName("dust").active = true;
            }else{
                this.roleNode.getChildByName("dust").active = false;
            }
            // console.log(this.isMovingList[0],keyControl.dropTable);
            if(this.isMovingList[0] && keyControl.dropTable){
                AudioManage.playSFX(soundList.jump,false,false,1);
                if(!this.openReversal){
                    this.roleNode.getComponent(cc.RigidBody).linearVelocity = cc.v2(speed.x,this.moveSpeed.y);
                }else{
                    this.roleNode.getComponent(cc.RigidBody).linearVelocity = cc.v2(speed.x,-this.moveSpeed.y);
                }
                keyControl.dropTable = false;
            }

            let spk = this.roleNode.getChildByName("spine").getComponent(sp.Skeleton);
            if(!this.isMovingList[1] && !this.isMovingList[2]){
                if(this.curAnimation == 1) return;
                this.curAnimation = 1;
                spk.setAnimation(0,"animation",true);
            }else{
                if(this.curAnimation == 2) return;
                this.curAnimation = 2;
                let curSkin = 0;
                if(gameData.randomSkinIndex != -1){
                    curSkin = gameData.randomSkinIndex;
                }else{
                    curSkin = gameData.getCurUseRoleSkin();
                }
                if(this.roleNode.getComponent("roleMediator").defaultSkin != -1){
                    curSkin = this.roleNode.getComponent("roleMediator").defaultSkin;
                }
                if(curSkin == 16 && gameData.lastColorfulList[1] == 0){
                    spk.setAnimation(0,"run",true);
                }else if(curSkin == 16 && gameData.lastColorfulList[1] == 1){
                    spk.setAnimation(0,"run1",true);
                }else{
                    spk.setAnimation(0,"run",true);
                }
            }
        }
    }

}
